<!-- 设备维修-维修列表 -->
<template>
  <basic-container class="production-page">
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      :defaults.sync="defaults"
      :page.sync="page"
      :permission="permissionList"
      :before-open="beforeOpen"
      v-model="form"
      ref="crud"
      @row-update="rowUpdate"
      @row-save="rowSave"
      @search-change="searchChange"
      @search-reset="searchReset"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template slot="menuLeft">
        <el-button type="warning" size="small" icon="el-icon-download" @click="$refs.exportComp.visible = true">导出</el-button>
      </template>
      <template slot="menuRight">
        <user-table-config :option="option" @confirm="(val) => handleSetUserConfig(val, pageCode)"></user-table-config>
      </template>
    </avue-crud>
    <export-comp ref="exportComp" page-api-url="mes-product/mrocenterequipment/query-page-repair-list" :option="option" :page="page" :query="query" file-name="维修列表">
    </export-comp>
  </basic-container>
</template>

<script>
import { mapGetters } from 'vuex';
import { getList, remove, getDetail } from '@/api/equipment-manage/maintenanceList';
import exportComp from '@/components/export-comp/index';
import userTableConfig from '@/components/user-table-config';
import userConfig from '@/mixins/userConfig';
export default {
  name: 'EquipmentManageMaintenanceList',
  components: {
    exportComp,
    userTableConfig
  },
  mixins: [userConfig()],
  data() {
    return {
      pageCode: 'MAINTENANCE_LIST',
      form: {},
      defaults: {},
      query: {},
      loading: true,
      page: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      },
      option: {
        height: 'calc(100vh - 440px)',
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchMenuSpan: 6,
        border: true,
        index: true,
        indexLabel: '序号',
        viewBtn: true,
        viewBtnText: '详情',
        viewTitle: '详情',
        //excelBtn: true, // 导出
        addBtn: false,
        editBtn: false,
        delBtn: false,
        selection: false,
        dialogClickModal: false,
        labelWidth: 120,
        menuWidth: 100,
        columnBtn: false,
        column: [
          {
            label: '维修单号',
            width: 150,
            prop: 'handlingNo',
            search: true,
            rules: [
              {
                required: true,
                message: '请输入维修单号',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '状态',
            prop: 'status',
            html: true,
            formatter: (val) => {
              switch (val.status) {
                case 2:
                  return `<span class="stateWords-warning">待维修</span>`;
                case 7:
                  return `<span class="stateWords-danger">维修中</span>`;
                case 8:
                  return `<span class="stateWords-success">维修结束</span>`;
                default:
                  break;
              }
            },
            dicData: [
              { label: '待维修', value: 2 },
              { label: '维修中', value: 7 },
              { label: '维修结束', value: 8 }
            ]
          },
          {
            label: '设备编码',
            prop: 'equipmentCode',
            search: true,
            rules: [
              {
                required: true,
                message: '请输入设备编码',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '设备名称',
            prop: 'equipmentName',
            search: true,
            rules: [
              {
                required: true,
                message: '请输入设备名称',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '紧急程度',
            width: 70,
            prop: 'urgency',
            type: 'select',
            value: 'GENERAL',
            dicData: [
              { label: '一般', value: 'GENERAL' },
              { label: '紧急', value: 'URGE' }
            ]
          },
          {
            label: '报修人',
            prop: 'repairApplicant'
          },
          {
            label: '报修时间',
            prop: 'repairTime'
          },
          {
            label: '故障类型',
            prop: 'faultType'
          },
          {
            label: '维修人',
            prop: 'handleBy'
          },
          {
            label: '维修时长',
            prop: 'durationFormatter'
          },
          {
            label: '维修开始时间',
            prop: 'startTime'
          },
          {
            label: '维修完成时间',
            prop: 'endTime'
          }
        ],
        group: [
          {
            label: '设备信息',
            prop: 'sbxx',
            column: [
              {
                label: '设备编码',
                prop: 'equipmentCode'
              },
              {
                label: '设备名称',
                prop: 'equipmentName'
              }
            ]
          },
          {
            label: '故障登记信息',
            prop: 'gzdjxx',
            column: [
              {
                label: '故障现象',
                prop: 'faultSymptom'
              },
              {
                label: '紧急程度',
                prop: 'urgency',
                type: 'select',
                value: 'GENERAL',
                dicData: [
                  { label: '一般', value: 'GENERAL' },
                  { label: '紧急', value: 'URGE' }
                ]
              },
              {
                label: '报修人',
                prop: 'repairApplicant'
              },
              {
                label: '报修时间',
                prop: 'repairTime'
              },
              {
                label: '故障描述',
                prop: 'remark',
                type: 'textarea',
                minRows: 3,
                maxRows: 3,
                span: 24
              },
              {
                label: '故障图片',
                prop: 'beforeRepairImageList',
                type: 'upload',
                listType: 'picture-card',
                span: 24,
                propsHttp: {
                  res: 'data',
                  url: 'link',
                  name: 'originalName'
                },
                props: {
                  label: '',
                  value: 'link'
                },
                tip: '只能上传png/jpeg/jpg文件且最多上传5张！',
                action: '/api/blade-resource/oss/endpoint/put-file-attach'
              }
            ]
          },
          {
            label: '维修执行信息',
            prop: 'gzdjxx',
            column: [
              {
                label: '故障类型',
                prop: 'faultType'
              },
              {
                label: '维修人员',
                prop: 'handleBy'
              },
              {
                label: '维修开始时间',
                prop: 'startTime'
              },
              {
                label: '维修完成时间',
                prop: 'endTime'
              },
              {
                label: '处理措施',
                prop: 'handlingAction',
                type: 'textarea',
                minRows: 3,
                maxRows: 3,
                span: 24
              },
              {
                label: '原因及改善',
                prop: 'suggestion',
                type: 'textarea',
                minRows: 3,
                maxRows: 3,
                span: 24
              },
              {
                label: '维修完成图片',
                prop: 'resultRepairImageList',
                type: 'upload',
                listType: 'picture-card',
                loadText: '附件上传中，请稍等',
                span: 24,
                propsHttp: {
                  res: 'data',
                  url: 'link',
                  name: 'originalName'
                },
                props: {
                  label: '',
                  value: 'link'
                },
                tip: '只能上传png/jpeg/jpg文件且最多上传3张！',
                action: '/api/blade-resource/oss/endpoint/put-file-attach'
              }
            ]
          }
        ]
      },
      data: []
    };
  },
  computed: {
    ...mapGetters(['permission']),
    permissionList() {
      return {
        // addBtn: this.vaildData(this.permission.productionline_add, false),
        viewBtn: this.vaildData(this.permission.maintenanceListView, false)
        // delBtn: this.vaildData(this.permission.productionline_delete, false),
        // editBtn: this.vaildData(this.permission.productionline_edit, false)
      };
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.$structuralSplit();
    });
  },
  methods: {
    rowSave(row, done, loading) {
      add(row).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '添加产线成功!'
          });
          done();
        },
        (error) => {
          loading();
          window.console.log(error);
        }
      );
    },
    rowUpdate(row, index, done, loading) {
      update(row).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '编辑产线成功!'
          });
          done();
        },
        (error) => {
          loading();
          console.log(error);
        }
      );
    },
    beforeOpen(done, type) {
      for (const key in this.defaults) {
        this.defaults[key].display = false;
      }
      if (type == 'view') {
        getDetail(this.form.id, this.form.handlingId).then((res) => {
          this.form = res.data.data;
        });
      }
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },

    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then((res) => {
        const data = res.data.data;
        if (data) {
          this.page.total = data.total;
          this.data = data.records;
        }
        this.loading = false;
      });
    }
  }
};
</script>

<style lang="scss" scope>
.production-page .avue-group__header {
  display: none;
}
</style>
